﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FunPokerMakerOnline.Business
{
    //TODO Commentary

    /// <summary>
    /// This class represent a collection of cards in the deck.
    /// </summary>
    public class Deck
    {

        #region "Variables"

        /// <summary>
        /// The List of cards in the deck.
        /// </summary>
        private List<Card> _DeckCard = new List<Card>();

        private int _IndexOfCardTodeal;

        #endregion

        #region "Properties"
        /// <summary>
        /// The number of cards that the deck has
        /// </summary>
        public int NumberOfCards
        {
            get { return this._DeckCard.Count(); }
        }

        #endregion

        #region "Constructors"
        /// <summary>
        /// Initializes a new instance of Deck using a list of cards.
        /// </summary>
        /// <param name="DeckCard"></param>
        public Deck(List<Card> DeckCard)
        {
            this._DeckCard = DeckCard;

        }
        #endregion

        #region "Methods"
        public void Shuffle()
        {
            this.Shuffle(1);
        }
        public void Shuffle(int NbOfShuffle)
        {
            Random rnd = new Random();

            int n = this._DeckCard.Count();
            for (int i = 0; i < NbOfShuffle; i++)
            {
                while (n > 1)
                {
                    int k = rnd.Next(n);
                    n--;
                    Card temp = this._DeckCard[n];
                    this._DeckCard[n] = this._DeckCard[k];
                    this._DeckCard[k] = temp;
                }
            }

        }

        /// <summary>
        /// This
        /// </summary>
        public void Shuffle(int NbOfShuffle, Boolean PickUpOneToOne)
        {

            Random rnd = new Random();
            List<Card> CardReleased;
            for (int i = 0; i < NbOfShuffle; i++)
            {

                CardReleased = new List<Card>();
                while (CardReleased.Count != (this._DeckCard.Count()))
                {
                    Card rndCard = this._DeckCard[rnd.Next(0, this._DeckCard.Count())];
                    if (!CardReleased.Contains(rndCard))
                    {
                        CardReleased.Add(rndCard);
                    }
                }
                this._DeckCard = CardReleased;
            }

        }

        /// <summary>
        /// This
        /// </summary>
        public void Shuffle(Boolean PickUpOneToOne)
        {

            Shuffle(1, true);
        }

        public Card Deal()
        {
            if (this._IndexOfCardTodeal >= this._DeckCard.Count())
            {
                throw new InvalidOperationException("All of the cards were delt");
            }
            return this._DeckCard[this._IndexOfCardTodeal++];
        }
        #endregion

    }
}
